package org.icress.fusion.moc

class TiController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        render view : '/moc/ti/list', model : [tiInstanceList: Ti.list(params), tiInstanceTotal: Ti.count()]
    }

    def create = {
        def tiInstance = new Ti()
        tiInstance.properties = params
        render view : '/moc/ti/create', model : [tiInstance: tiInstance]
    }

    def save = {
        def tiInstance = new Ti(params)
		tiInstance.validate()
        if (!tiInstance.hasErrors() && tiInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'moc.ti.label', default: 'Ti'), tiInstance.id])}"
            redirect(action: "show", id: tiInstance.id)
        }
        else {
            render(view: "/moc/ti/create", model: [tiInstance: tiInstance])
        }
    }

    def show = {
        def tiInstance = Ti.get(params.id)
        if (!tiInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.ti.label', default: 'Ti'), params.id])}"
            redirect(action: "list")
        }
        else {
            render(view: "/moc/ti/show", model: [tiInstance: tiInstance])
        }
    }

    def edit = {
        def tiInstance = Ti.get(params.id)
        if (!tiInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.ti.label', default: 'Ti'), params.id])}"
            redirect(action: "list")
        }
        else {
            render view : '/moc/ti/edit', model : [tiInstance: tiInstance]
        }
    }

    def update = {
        def tiInstance = Ti.get(params.id)
        if (tiInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (tiInstance.version > version) {
                    
                    tiInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'moc.ti.label', default: 'Ti')] as Object[], "Another user has updated this Ti while you were editing")
                    render(view: "edit", model: [tiInstance: tiInstance])
                    return
                }
            }
            tiInstance.properties = params
            if (!tiInstance.hasErrors() && tiInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'moc.ti.label', default: 'Ti'), tiInstance.id])}"
                redirect(action: "show", id: tiInstance.id)
            }
            else {
                render(view: "/moc/ti/edit", model: [tiInstance: tiInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.ti.label', default: 'Ti'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def tiInstance = Ti.get(params.id)
        if (tiInstance) {
            try {
                tiInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'moc.ti.label', default: 'Ti'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'moc.ti.label', default: 'Ti'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'moc.ti.label', default: 'Ti'), params.id])}"
            redirect(action: "list")
        }
    }
}
